package com.weixing.mall.provider.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weixing.mall.base.constant.GlobalConstant;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.AgentAuth;
import com.weixing.mall.core.support.auth.AuthUtil;
import com.weixing.mall.core.support.auth.SysAuth;
import com.weixing.mall.provider.api.model.vo.RoleVo;
import com.weixing.mall.provider.api.service.SysUserRpcService;
import com.weixing.mall.provider.model.query.AgentRebateQuery;
import com.weixing.mall.provider.model.vo.AgentRebateVo;
import com.weixing.mall.provider.model.vo.AgentRebateWithdrawVo;
import com.weixing.mall.provider.service.IAgentRebateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 代理商返利Api
 *
 * @author  gjq
 * @date  2019/10/16
 * @since  1.0
 **/
@RestController
@RequestMapping("agent/rebate")
public class AgentRebateController extends BaseController {

    @Autowired
    private IAgentRebateService agentRebateService;
    @Autowired
    private SysUserRpcService sysUserRpcService;

    /**
     * 代理商返利列表--客服用
     *
     * @param query  查询参数
     * @return       返利列表
     */
    @GetMapping("")
    public Result<IPage<AgentRebateVo>> list( AgentRebateQuery query){
        SysAuth auth = AuthUtil.getSysExt();
        Long uid=auth.getUid();
        IPage<AgentRebateVo> pageInfo=null;
        List<RoleVo> list= sysUserRpcService.getRoleById(uid).getData();
        for(RoleVo vo:list){
            if(vo.getRoleCode().equals(GlobalConstant.RoleCode.ADMIN)){
                pageInfo = agentRebateService.selectListOfPage(initMpPage(),query);
                return ResultUtil.success(pageInfo);
            }
        }
        for(RoleVo vo:list){
            if(vo.getRoleCode().equals(GlobalConstant.RoleCode.KFADMIN)){
                pageInfo = agentRebateService.selectListOfPage(initMpPage(),query);
                return ResultUtil.success(pageInfo);
            }
        }
        query.setCustomerId(uid.toString());
         pageInfo = agentRebateService.selectListOfPage(initMpPage(),query);
        return ResultUtil.success(pageInfo);
    }

    /**
     * 通过id查看代理商返利
     *
     * @param id       返利id
     * @return         返利
     */
    @GetMapping("/{id}")
    public Result<AgentRebateVo> getAgentRebate(@PathVariable long id){
        AgentRebateVo agentRebateVo= agentRebateService.rebateDetail(id);
        return ResultUtil.success(agentRebateVo);
    }

    /**
     * 通过代理商提现id查看代理商返利（状态为有效已付款）
     * @param id      提现id
     * @return
     */
    @GetMapping("rebateById/{id}")
    public Result<IPage<AgentRebateWithdrawVo>> getAgentRebateByWithdrawId(@PathVariable long id){
        IPage<AgentRebateWithdrawVo> pageInfo = agentRebateService.getAgentRebateByWithdrawId(initMpPage(),id);
        return ResultUtil.success(pageInfo);
    }

    /**
     * 加载代理商返利列表--代理用
     *
     * @param query    查询条件
     * @return          返利列表
     */
    @GetMapping("/agent")
    public Result<IPage<AgentRebateVo>> listForAgent(AgentRebateQuery query){
        AgentAuth auth = AuthUtil.getAgentExt();
        query.setAgentId(auth.getAid());
        IPage<AgentRebateVo> pageInfo = agentRebateService.selectListOfPage(initMpPage(),query);
        return ResultUtil.success(pageInfo);
    }

}
